package com.cctv.user.controller;

import com.cctv.user.constant.CookieConstant;
import com.cctv.user.constant.RedisConstant;
import com.cctv.user.dataobject.UserInfo;
import com.cctv.user.enums.ResultEnum;
import com.cctv.user.enums.RoleEnum;
import com.cctv.user.service.UserService;
import com.cctv.user.utils.CookieUtil;
import com.cctv.user.utils.ResultVOUtil;
import com.cctv.user.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 登录
 *
 * @Author: cctv
 * @Date: 2019/6/12 17:11
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 买家登录
     *
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid, HttpServletResponse response) {
        // 1.openid 查找用户数据
        UserInfo userInfo = userService.findByWxOpenid(openid);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        // 2.判断角色
        if (!userInfo.getRole().equals(RoleEnum.BUYER.getCode())) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        // 3.cookie=abc
        CookieUtil.set(response, CookieConstant.OPENID, openid, CookieConstant.expire);
        return ResultVOUtil.success();

    }

    /**
     * 卖家登录
     *
     * @param openid
     * @param response
     * @return
     */
    @GetMapping("/seller")
    @CrossOrigin(allowCredentials = "true")
    public ResultVO seller(@RequestParam("openid") String openid, HttpServletRequest request, HttpServletResponse response) {
        // 判断是否已登录, 防止产生多个重复的uuid
        String cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null &&
                !StringUtils.isEmpty(stringRedisTemplate.opsForValue().get(
                        String.format(RedisConstant.TOKEN_TEMPLATE, cookie)))) {
            return ResultVOUtil.success();
        }
        // 1.openid 查找用户数据
        UserInfo userInfo = userService.findByWxOpenid(openid);
        if (userInfo == null) {
            return ResultVOUtil.error(ResultEnum.LOGIN_FAIL);
        }
        // 2.判断角色
        if (!userInfo.getRole().equals(RoleEnum.SELLER.getCode())) {
            return ResultVOUtil.error(ResultEnum.ROLE_ERROR);
        }
        // 3.redis设置key=UUID value=xyz
        String token = UUID.randomUUID().toString();
        Integer expire = CookieConstant.expire;
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_TEMPLATE, token),
                openid,
                expire,
                TimeUnit.SECONDS);

        // 4.cookie里设置token=UUID

        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.expire);
        return ResultVOUtil.success();
    }
}
